package Plan;
import java.awt.*;
import java.awt.event.*;

public class Player {
    private int x, y;
    private int speed = 5;
    private boolean leftPressed, rightPressed, upPressed, downPressed;
    private boolean firePressed;
    private long lastFireTime;
    private int fireDelay = 200;
    
    public Player(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                leftPressed = true;
                break;
            case KeyEvent.VK_RIGHT:
                rightPressed = true;
                break;
            case KeyEvent.VK_UP:
                upPressed = true;
                break;
            case KeyEvent.VK_DOWN:
                downPressed = true;
                break;
            case KeyEvent.VK_SPACE:
                firePressed = true;
                break;
        }
    }
    
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                leftPressed = false;
                break;
            case KeyEvent.VK_RIGHT:
                rightPressed = false;
                break;
            case KeyEvent.VK_UP:
                upPressed = false;
                break;
            case KeyEvent.VK_DOWN:
                downPressed = false;
                break;
            case KeyEvent.VK_SPACE:
                firePressed = false;
                break;
        }
    }
    
    public void update() {
        if (leftPressed) x = Math.max(0, x - speed);
        if (rightPressed) x = Math.min(750, x + speed);
        if (upPressed) y = Math.max(0, y - speed);
        if (downPressed) y = Math.min(550, y + speed);
    }
    
    public Bullet fire() {
        long currentTime = System.currentTimeMillis();
        if (firePressed && currentTime - lastFireTime > fireDelay) {
            lastFireTime = currentTime;
            return new Bullet(x + 25, y - 10);
        }
        return null;
    }
    
    public void draw(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillRect(x, y, 50, 50);
    }
    
    public Rectangle getBounds() {
        return new Rectangle(x, y, 50, 50);
    }
}